/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Admin_ActuVuelos.java
 *
 * Created on 25/09/2010, 14:38:12
 */

package Vista.Administrador;
import java.awt.Component;
import java.util.Date;

import Controlador.*;
import Modelo.*;

import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Foox
 */
public class Admin_ActuVuelos extends javax.swing.JFrame {
    
    private ControladorVuelos conVuelos=new ControladorVuelos();
    private Vuelo auxVuelo= new Vuelo();
    /** Creates new form Admin_ActuVuelos */
    public Admin_ActuVuelos() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        IDText = new javax.swing.JTextField();
        buscar = new javax.swing.JButton();
        jLabel = new javax.swing.JLabel();
        idVueloText = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        destinoText = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        fechaText = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        horaText = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        Guardar = new javax.swing.JButton();
        Cancelar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        precioTuri = new javax.swing.JTextField();
        precioVip = new javax.swing.JTextField();
        eliminar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        promoBox = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        tipoBox = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        mostrar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        origenText = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Vuelos");

        model = new javax.swing.table.DefaultTableModel(null,new String [] {"ID", "Origen", "Destino", "Fecha" }); 
        jTable1.setModel(model);
        
        /*jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Id", "Origen", "Destino", "Fecha", "Hora"
            }
        ));*/
        jScrollPane1.setViewportView(jTable1);

        jLabel2.setText("Buscar");

        buscar.setText("jButton1");
        buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarActionPerformed(evt);
            }
        });

        jLabel.setText("idVuelo");

        idVueloText.setBackground(new java.awt.Color(204, 204, 204));

        jLabel4.setText("Destino");

        destinoText.setBackground(new java.awt.Color(204, 204, 204));

        jLabel5.setText("Fecha");

        fechaText.setBackground(new java.awt.Color(204, 204, 204));

        jLabel6.setText("Hora");

        horaText.setBackground(new java.awt.Color(204, 204, 204));

        jLabel7.setText("Promo");

        Guardar.setText("Guardar");
        Guardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GuardarActionPerformed(evt);
            }
        });

        Cancelar.setText("Cancel");
        Cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelarActionPerformed(evt);
            }
        });

        jLabel8.setText("Precio turista");

        jLabel9.setText("Precio vip");

        precioTuri.setBackground(new java.awt.Color(204, 204, 204));

        precioVip.setBackground(new java.awt.Color(204, 204, 204));
        precioVip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                precioVipActionPerformed(evt);
            }
        });

        eliminar.setText("Eliminar");
        eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarActionPerformed(evt);
            }
        });

        promoBox.setModel(new javax.swing.DefaultComboBoxModel(ControladorPromo.getPromociones()));

        jLabel10.setText("Tipo");

        tipoBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Largo", "Corto" }));
        tipoBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipoBoxActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(153, 153, 153));
        jLabel1.setText("Vuelos:");

        mostrar.setText("Mostrar");
        mostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mostrarActionPerformed(evt);
            }
        });

        jLabel3.setText("Origen");

        origenText.setBackground(new java.awt.Color(204, 204, 204));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addContainerGap(479, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(Guardar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Cancelar)
                        .addContainerGap())
                    .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(mostrar)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(jLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(idVueloText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                            .addComponent(fechaText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                            .addComponent(origenText, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                            .addComponent(destinoText, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel6)
                            .addComponent(jLabel9)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(horaText, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                                    .addComponent(precioVip)
                                    .addComponent(precioTuri))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel10)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tipoBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(promoBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(80, 80, 80))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(IDText, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buscar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(300, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(IDText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(mostrar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel)
                    .addComponent(idVueloText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(precioTuri, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(tipoBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(precioVip, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(origenText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(horaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(destinoText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(promoBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(fechaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Cancelar)
                    .addComponent(Guardar)
                    .addComponent(eliminar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tipoBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipoBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tipoBoxActionPerformed

    private void MostrarDatos(Vuelo v){
        	idVueloText.setText(""+v.getId());
        	origenText.setText(v.getOrigen());
            destinoText.setText(v.getDestino());
            fechaText.setText(v.getFecha());
            precioTuri.setText(v.getPrecioTurista());
            precioVip.setText(v.getPrecioVip());
            promoBox.setSelectedIndex(2);
            if(auxVuelo.getTipo_vuelo()=="Largo")tipoBox.setSelectedIndex(0);
            if(auxVuelo.getTipo_vuelo()=="Corto")tipoBox.setSelectedIndex(1);
    }
    public void removerFilas() {
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        modelo.setNumRows(0);
    }

    private void buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarActionPerformed
        // TODO add your handling code here:
    	
        //ControladorVuelos u=new ControladorVuelos();
        
        Vuelo[]vuelos;
        DefaultTableModel modelo = (DefaultTableModel) jTable1.getModel();
        
        if(IDText.getText()==null){
        
        vuelos=ControladorVuelos.getListadoVuelos(IDText.getText());
        if(vuelos[0]!=null){
       
              
        for(int i=0;i<vuelos.length-1;i++){
        	
        	Object[]aux ={vuelos[i],vuelos[i].getOrigen(),vuelos[i].getDestino(),vuelos[i].getFecha()};        
        	modelo.addRow(aux);
        
        }
        jTable1.repaint();
        
        }
        }else{
        	
        	vuelos=ControladorVuelos.getListadoVuelos(IDText.getText());
        	
        	if(vuelos!=null){
        	     
        		
        		
                for(int i=0;i<vuelos.length-1;i++){
                	
                	Object[]aux ={vuelos[i],vuelos[i].getOrigen(),vuelos[i].getDestino(),vuelos[i].getFecha()};        
                	modelo.addRow(aux);
                
                }
                jTable1.repaint();
        		
        	}
        	
        	
        	
        }
        
       

    }//GEN-LAST:event_buscarActionPerformed
    
    private void precioVipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_precioVipActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_precioVipActionPerformed

    private void GuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GuardarActionPerformed
        // TODO add your handling code here:
        	auxVuelo=new Vuelo();
        	auxVuelo.setAvion(""+idVueloText.getText());
            auxVuelo.setId(Integer.parseInt(idVueloText.getText()));
            auxVuelo.setDestino(destinoText.getText());
            auxVuelo.setOrigen(origenText.getText());
            auxVuelo.setFecha(fechaText.getText()+ " " +horaText.getText());
            auxVuelo.setPrecioTurista(precioTuri.getText());
            auxVuelo.setPrecioVip(precioVip.getText());
            auxVuelo.setPromociones((Promociones)promoBox.getSelectedItem());
            ControladorVuelos.crearVuelo(auxVuelo);
            this.dispose();
    }//GEN-LAST:event_GuardarActionPerformed

    private void CancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelarActionPerformed
        // TODO add your handling code here:
    	this.dispose();
    }//GEN-LAST:event_CancelarActionPerformed

    private void eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarActionPerformed
        // TODO add your handling code here:
            idVueloText.setText("");
            destinoText.setText("");
            origenText.setText("");
            fechaText.setText("");
            precioTuri.setText("");
            precioVip.setText("");
            horaText.setText("");
            //Modificar = -1;
            if(auxVuelo!=null){
                System.out.println(conVuelos.eliminarVuelo(auxVuelo));
            }
    }//GEN-LAST:event_eliminarActionPerformed

    private void mostrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mostrarActionPerformed
        // TODO add your handling code here:
    	
    	int index = jTable1.getSelectedRow();
        if (index != -1) {
            Vuelo p = (Vuelo) jTable1.getValueAt(index, 0);
            MostrarDatos(p);
            
        }
    	
    }//GEN-LAST:event_mostrarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Admin_ActuVuelos().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Cancelar;
    private javax.swing.JButton Guardar;
    private javax.swing.JTextField IDText;
    private javax.swing.JButton buscar;
    private javax.swing.JTextField destinoText;
    private javax.swing.JButton eliminar;
    private javax.swing.JTextField fechaText;
    private javax.swing.JTextField horaText;
    private javax.swing.JTextField idVueloText;
    private javax.swing.JLabel jLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton mostrar;
    private javax.swing.JTextField origenText;
    private javax.swing.JTextField precioTuri;
    private javax.swing.JTextField precioVip;
    private javax.swing.JComboBox promoBox;
    private javax.swing.JComboBox tipoBox;
    private javax.swing.table.DefaultTableModel model;
    // End of variables declaration//GEN-END:variables

}
